/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.services.search.navigation;

import com.kcp.ko.fmtnmlib.hibernate.QueryFactory;
import org.braintrain.domain.Module;
import org.braintrain.domain.Navigation;
import org.hibernate.Query;
import org.hibernate.Session;

import java.util.List;

/**
 * One cornerstone of BrainTrain.  This Factory determines first item(s) in a module along the main navigation
 * chain or the linked chains.  Also, takes into account deleted navigation and changed (or moved navigation).
 * 
 * @author John Ericksen
 *
 */

public class StartingNavigationQueryFactory implements QueryFactory<Navigation> {

    private Module module;
    private boolean mainNavigation;
    private boolean includeDeleted;
    private boolean deltaChain;
    private List<Long> notInList;
    
    public StartingNavigationQueryFactory(Module module, boolean mainNavigation, boolean deltaChain, boolean includeDeleted)
    {
        this.module = module;
        this.mainNavigation = mainNavigation;
        this.deltaChain = deltaChain;
        this.includeDeleted = includeDeleted;
    }
    
    public StartingNavigationQueryFactory(Module module, boolean mainNavigation, boolean deltaChain, boolean includeDeleted, List<Long> notInList)
    {
        this(module, mainNavigation, deltaChain, includeDeleted);
        this.notInList = notInList;
    }
    
    public Query getQuery(Session session) {
        
        StringBuffer queryString = new StringBuffer();
        
        //Module id and mainNaviation must be specified
        queryString.append("From Navigation n where " +
                           "n.module = :module " +
                           "and n.mainNavigation = :mainNavigation ");
        
        //Include the deleted items?
        if(!includeDeleted)
        {
            queryString.append("and n.deleted = false ");
        }
        
        //Along the delta path or released path?
        if(deltaChain)
        {
            queryString.append("and n.bundle.deltaValue.prevSet is empty ");
            //queryString.append("and n.deleted = false ");
        }
        else
        {
            queryString.append("and n.bundle.value.prevSet is empty ");
            queryString.append("and n.pending = false ");
            
        }
        
        //and if you are searching for the linked chains....
        if(notInList != null && notInList.size() > 0)
        {
            queryString.append("and n.id not in (:notInList) ");
            
        }
        
        queryString.append("order by name");
        
        Query query = session.createQuery(queryString.toString());
        
        query.setEntity("module", module);
        query.setBoolean("mainNavigation", mainNavigation);

        if(notInList != null && notInList.size() > 0)
        {
            query.setParameterList("notInList", notInList);
        }
        
        return query;
    }
}